package cn.anc.aonicardv.net.download;

import android.net.Network;
import android.text.TextUtils;

import java.io.File;

import cn.anc.aonicardv.BuildConfig;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.MyApplication;
import cn.anc.aonicardv.net.ApiManager;
import cn.anc.aonicardv.net.NetRequestClient;
import cn.anc.aonicardv.util.FileUtils;
import cn.anc.aonicardv.util.LogUtil;
import rx.Subscription;

/**
 * Created by yangdai on 2017/6/6.
 */

public class DownLoadManager {

    private static DownLoadManager downLoadManager;
    private ApiManager apiManager;
    private NetRequestClient.Builder builder;
    private NetRequestClient netRequestClient;
    public static final String DOWNLOAD_PATH_ROOT = FileUtils.getSDPath(MyApplication.getInstance()) + "/" + BuildConfig.FLAVOR + "/download";
    public static final String PHOTO_DIR = "/DCIM";
    public static final String NORMAL_VIDEO_DIR = "/REC";
    public static final String URGENT_VIDEO_DIR = "/EVENT";
    public static String PHOTO_PATH = DOWNLOAD_PATH_ROOT + PHOTO_DIR;
    public static String NORMAL_VIDEO_PATH = DOWNLOAD_PATH_ROOT + NORMAL_VIDEO_DIR;
    public static String URGENT_VIDEO_PATH = DOWNLOAD_PATH_ROOT + URGENT_VIDEO_DIR;
    public String baseUrl;
    private Network network;

    public static synchronized DownLoadManager getInstance() {
        if (downLoadManager == null) {
            downLoadManager = new DownLoadManager();
        }
        return downLoadManager;
    }

    public DownLoadManager() {
        builder = new NetRequestClient.Builder();
    }

    public DownLoadManager baseUrl(String url) {
        if (!TextUtils.isEmpty(url)) {
            baseUrl = url;
        }
        return downLoadManager;
    }

    public DownLoadManager setNetwork(Network network) {
        if (network != null) {
            this.network = network;
        }
        return downLoadManager;
    }

    private ApiManager getApiManager() {
        return netRequestClient.getRetrofit().create(ApiManager.class);
    }

    public void settingOk() {
        netRequestClient = builder.baseUrl(baseUrl).setNetwork(network).build();
        apiManager = getApiManager();
    }

    public Subscription download(String url, String dstPath, DownLoadCallBack downLoadCallBack) {

        if (TextUtils.isEmpty(url))
            return null;
        String saveDir = dstPath.substring(0, dstPath.lastIndexOf("/") + 1);
        String filename = dstPath.substring(dstPath.lastIndexOf("/") + 1, dstPath.length());
        File dir = new File(saveDir);
        if (!dir.exists())
            dir.mkdirs();
        return executeDownload(url, saveDir, filename, downLoadCallBack);
    }

    private Subscription executeDownload(String url, String savePath, String name, DownLoadCallBack callBack) {
        return netRequestClient.execute(apiManager.downloadFile(url), new DownLoadSubscriber(savePath, name, callBack, MyApplication.getInstance()));
    }

    public String getDownloadPathByRemotePath(int type, String path) {
        if (TextUtils.isEmpty(path) || !path.contains("/"))
            return "";
        String[] temp = path.split("/");
        String dir;
        if (type == Constant.DOWNLOAD.TYPE_PHOTO) {
            dir = DOWNLOAD_PATH_ROOT + "/" + PHOTO_DIR;
        } else if (type == Constant.DOWNLOAD.TYPE_EVENT) {
            dir = DOWNLOAD_PATH_ROOT + "/" + URGENT_VIDEO_DIR;
        } else if (type == Constant.DOWNLOAD.TYPE_REC) {
            dir = DOWNLOAD_PATH_ROOT + "/" + NORMAL_VIDEO_DIR;
        } else {
            return "";
        }
        File dirFile = new File(dir);
        if (!dirFile.exists())
            dirFile.mkdirs();
        LogUtil.e("-------getDownloadPathByRemotePath---------","--type----:"+type+"-----dir-------:"+dir + "/" + temp[temp.length - 1]);
        return dir + "/" + temp[temp.length - 1];
    }


}
